package bb.lanxing.util.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import androidx.appcompat.widget.AppCompatTextView;
import com.umeng.analytics.pro.d;
import bb.lanxing.R;
import bb.lanxing.util.DensityUtil;
import kotlin.Metadata;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.Intrinsics;

public final class ImageTextView extends AppCompatTextView {
    public static final int BOTTOM = 3;
    public static final Companion Companion = new Companion(null);
    public static final int LEFT = 0;
    public static final int RIGHT = 2;
    public static final int TOP = 1;
    private int defaultSize;
    private int mBottomHeight;
    private int mBottomWidth;
    private int mLeftHeight;
    private int mLeftWidth;
    private int mRightHeight;
    private int mRightWidth;
    private int mTopHeight;
    private int mTopWidth;

    public ImageTextView(Context context) {
        this(context, null, 2, null);
        Intrinsics.checkNotNullParameter(context, "context");
    }

    public /* synthetic */ ImageTextView(Context context, AttributeSet attributeSet, int i, DefaultConstructorMarker defaultConstructorMarker) {
        this(context, (i & 2) != 0 ? null : attributeSet);
    }

    public ImageTextView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        Intrinsics.checkNotNullParameter(context, "context");
        int dp2px = DensityUtil.dp2px(8.0f);
        this.defaultSize = dp2px;
        this.mLeftWidth = dp2px;
        this.mLeftHeight = dp2px;
        this.mTopWidth = dp2px;
        this.mTopHeight = dp2px;
        this.mRightWidth = dp2px;
        this.mRightHeight = dp2px;
        this.mBottomWidth = dp2px;
        this.mBottomHeight = dp2px;
        /*TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.ImageTextView);
        Intrinsics.checkNotNullExpressionValue(obtainStyledAttributes, "context.obtainStyledAttr….styleable.ImageTextView)");
        this.mLeftWidth = obtainStyledAttributes.getDimensionPixelOffset(3, 0);
        this.mLeftHeight = obtainStyledAttributes.getDimensionPixelOffset(2, 0);
        this.mTopWidth = obtainStyledAttributes.getDimensionPixelOffset(7, 0);
        this.mTopHeight = obtainStyledAttributes.getDimensionPixelOffset(6, 0);
        this.mRightWidth = obtainStyledAttributes.getDimensionPixelOffset(5, 0);
        this.mRightHeight = obtainStyledAttributes.getDimensionPixelOffset(4, 0);
        this.mBottomWidth = obtainStyledAttributes.getDimensionPixelOffset(1, 0);
        this.mBottomHeight = obtainStyledAttributes.getDimensionPixelOffset(0, 0);
        obtainStyledAttributes.recycle();*/
        applyDrawablesSize();
    }

    public static final class Companion {
        public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) {
            this();
        }

        private Companion() {
        }
    }

    private final void applyDrawablesSize() {
        Drawable[] compoundDrawables = getCompoundDrawables();
        Intrinsics.checkNotNullExpressionValue(compoundDrawables, "compoundDrawables");
        int length = compoundDrawables.length;
        for (int i = 0; i < length; i++) {
            if (i == 0) {
                setDrawableBounds(compoundDrawables[0], this.mLeftWidth, this.mLeftHeight);
            } else if (i == 1) {
                setDrawableBounds(compoundDrawables[1], this.mTopWidth, this.mTopHeight);
            } else if (i == 2) {
                setDrawableBounds(compoundDrawables[2], this.mRightWidth, this.mRightHeight);
            } else if (i == 3) {
                setDrawableBounds(compoundDrawables[3], this.mBottomWidth, this.mBottomHeight);
            }
        }
        setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], compoundDrawables[2], compoundDrawables[3]);
    }

    public final void setDrawable(int i, Drawable drawable, int i2, int i3) {
        if (drawable == null) {
            return;
        }
        Drawable[] compoundDrawables = getCompoundDrawables();
        Intrinsics.checkNotNullExpressionValue(compoundDrawables, "compoundDrawables");
        setDrawableBounds(drawable, i2, i3);
        compoundDrawables[i] = drawable;
        setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], compoundDrawables[2], compoundDrawables[3]);
    }

    public final void removeDrawable(int i) {
        Drawable[] compoundDrawables = getCompoundDrawables();
        Intrinsics.checkNotNullExpressionValue(compoundDrawables, "compoundDrawables");
        compoundDrawables[i] = null;
        setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], compoundDrawables[2], compoundDrawables[3]);
    }

    public final void setDrawablesSize(int i, int i2, int i3) {
        Drawable[] compoundDrawables = getCompoundDrawables();
        Intrinsics.checkNotNullExpressionValue(compoundDrawables, "compoundDrawables");
        if (i == 0) {
            this.mLeftWidth = i2;
            this.mLeftHeight = i3;
            setDrawableBounds(compoundDrawables[0], i2, i3);
        } else if (i == 1) {
            this.mTopWidth = i2;
            this.mTopHeight = i3;
            setDrawableBounds(compoundDrawables[1], i2, i3);
        } else if (i == 2) {
            this.mRightWidth = i2;
            this.mRightHeight = i3;
            setDrawableBounds(compoundDrawables[2], i2, i3);
        } else if (i == 3) {
            this.mBottomWidth = i2;
            this.mBottomHeight = i3;
            setDrawableBounds(compoundDrawables[3], i2, i3);
        }
        setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], compoundDrawables[2], compoundDrawables[3]);
    }

    private final void setDrawableBounds(Drawable drawable, int i, int i2) {
        if (drawable != null) {
            double intrinsicHeight = drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
            drawable.setBounds(0, 0, i, i2);
            Rect bounds = drawable.getBounds();
            Intrinsics.checkNotNullExpressionValue(bounds, "drawable.bounds");
            if (bounds.right == 0 && bounds.bottom == 0) {
                return;
            }
            if (bounds.right == 0) {
                bounds.right = (int) (bounds.bottom / intrinsicHeight);
                drawable.setBounds(bounds);
            }
            if (bounds.bottom != 0) {
                return;
            }
            bounds.bottom = (int) (bounds.right * intrinsicHeight);
            drawable.setBounds(bounds);
        }
    }
}
